<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  title = @context.is_a?(Course) ? t('content_exports', "Content Exports") : t('user_data_exports', "Download Submissions")
  friendly_message = @context.is_a?(Course) ? nil : t("Creating a Submission Export will allow you to download a zip file of all your assignment submissions in all of your present and past courses. This could be a big file, so it may take a while. You can navigate away from the page and come back later.")
%>


<% add_crumb title %>
<% content_for :page_title do %><%= title %><% end %>

<div>
  <h1><%= title %></h1>

  <% if friendly_message %>
    <p><%= friendly_message %></p>
  <% end %>

  <div class="export_messages" style="<%= hidden %>">
    <div class="alert-error error_message"></div>
  </div>

  <hr />

  <div id="export_files">
    <% @exports.each do |export| %>
      <% unless export.running? %>
        <% if export.workflow_state == 'exported' %>
          <p>
            <% date = "<span class=\"created_at time_ago_date\" data-timestamp=\"#{export.created_at.iso8601}\">#{datetime_string export.created_at}</span>".html_safe %>
            <% wrapper = link_to('\1', export.attachment ? verified_file_download_url(export.attachment) : nil) %>
            <% if export.expired? %>
              <%= t "Course Export from %{date} (file not available)", :date => date %>
            <% elsif export.export_type == ContentExport::COMMON_CARTRIDGE %>
              <%= t 'links.download', "Course Export from %{date}: *Click here to download*", :date => date, :wrapper => wrapper %>
            <% elsif export.export_type == ContentExport::QTI %>
              <%= t 'links.download_qti', "QTI Export from %{date}: *Click here to download*", :date => date, :wrapper => wrapper %>
            <% elsif export.export_type == ContentExport::USER_DATA %>
              <%= t 'links.download_user_export', "User Data Export from %{date}: *Click here to download*", :date => date, :wrapper => wrapper %>
            <% elsif export.export_type == ContentExport::ZIP %>
                <%= t 'links.download_file_export', "File Export from %{date}: *Click here to download*", :date => date, :wrapper => wrapper %>
            <% end %>
          </p>
        <% end %>
      <% end %>
    <% end %>
  </div>

  <% if ContentExport.expire? %>
    <p><em><%= t "Content export files cannot be downloaded after %{days} days.", days: ContentExport.expire_days %></em></p>
  <% end %>

  <hr />

  <div class="form" style="<%= hidden if @current_export_id %>">
    <% if @context.is_a?(Course) %>
      <%= form_tag course_content_exports_path(@context.id), :id => "exporter_form" do %>
        <fieldset>
          <h2><%= t :export_type, "Export Type" %></h2>
          <div>
            <input type="radio" name="export_type" value="common_cartridge" id="cc_export_radio" checked>
            <label for="cc_export_radio"><%= t 'labels.course', 'Course' %></label>
          </div>
          <div>
            <input type="radio" name="export_type" value="qti" id="qti_export_radio">
            <label for="qti_export_radio"><%= t 'labels.quiz', 'Quiz' %></label>
            <div id="quiz_selection" style="display:none;">
              {{> views/quiz_export_checklist}}
            </div>
          </div>
        </fieldset>
        <div>
          <button type="submit" class="submit_button btn btn-primary"><%= t 'buttons.create', "Create Export" %></button>
        </div>
        <div class="clear"></div>
      <% end %>
    <% elsif @context == @current_user %>
      <% if @current_user.submissions.exists? %>
        <%= form_tag dashboard_content_exports_path, :id => "exporter_form" do %>
          <div>
            <button type="submit" class="submit_button btn btn-primary"><%= t 'buttons.create', "Create Export" %></button>
          </div>
          <div class="clear"></div>
        <% end %>
      <% else %>
        {{#t}}You currently have no submissions to export.{{/t}}
      <% end %>
    <% end %>
  </div>

  <div class="progress_bar_holder" style="<%= hidden unless @current_export_id %> margin-top: 10px;">
      <div class="export_progress"></div>
      <% if @context.is_a?(Course) %>
        <div class="progress_message">
          <%= mt 'message.export_started', "The export process has started. This can take awhile for large courses. *You can leave the page* and you'll get an email when the export is complete." %>
        </div>
      <% end %>
  </div>

  <% if @current_export_id %>
    <div id="current_export_id" style="<%= hidden %>"><%= @current_export_id %></div>
  <% end %>
</div>

<% js_bundle :content_exports %>
